let username = getKey("is").username;
let password;
const numCharEN = /^[A-Za-z\d]+$/;

// 检查登陆状态
function isLogin() {
    if (localStorage.getItem(username) == null) {
        window.location.href = "../html/login.html";
        alert("⚠当前账号未注册");
    } else if (localStorage.getItem("is") == null || JSON.parse(localStorage.getItem("is")).isLogin !== "true") {
        window.location.href = "../html/login.html";
        alert("当前未登录，即将返回登录页面");
    }
}

let oldPasswordResult = false;
let newPasswordResult = false;
let rePasswordResult = false;


// 检查输入
function check() {
    let oldPassword = getDocValue("oldPassword");
    let newPassword = getDocValue("newPassword");
    let rePassword = getDocValue("rePassword");

    checkOldPassword(oldPassword);
    checkNewPassword(oldPassword, newPassword);
    checkRePassword(newPassword, rePassword);

    if (oldPasswordResult && newPasswordResult && rePasswordResult) {
        password = newPassword;
        setNewPassword(username);
        alert("密码修改成功！即将返回主页！");
    } else return false;
}

// 检查原密码
function checkOldPassword(oldPassword) {
    if (oldPassword === "" || oldPassword == null) {
        printError("oldPasswordErr", "请输入原密码");
    } else if (getKey(username).password !== oldPassword) {
        printError("oldPasswordErr", "原密码错误");
    } else {
        printError("oldPasswordErr", "");
        oldPasswordResult = true;
    }
}

// 检查修改后的密码
function checkNewPassword(oldPassword, newPassword) {
    if (newPassword === "" || newPassword == null) {
        printError("newPasswordErr", "新密码不能为空");
    } else if (!(newPassword.length >= 6 && newPassword.length <= 20)) {
        printError("newPasswordErr", "密码长度应当在6-20个字符内");
    } else if (!(numCharEN.test(newPassword))) {
        printError("newPasswordErr", "密码应当仅由数字或字母组合");
    } else if (newPassword === oldPassword) {
        printError("newPasswordErr", "新设置的密码不能与原密码相同");
    } else {
        printError("newPasswordErr", "");
        newPasswordResult = true;
    }
}

// 检验确认密码
function checkRePassword(newPassword, rePassword) {
    if (rePassword === "" || rePassword == null) {
        printError("rePasswordErr", "确认密码不能为空");
    } else if (rePassword !== newPassword) {
        printError("rePasswordErr", "两次密码不一致");
    } else {
        printError("rePasswordErr", "");
        rePasswordResult = true;
    }
}


// 储存
function setNewPassword(username) {
    let account = JSON.stringify({
        "username": getKey(username).username,
        "name": getKey(username).name,
        "email": getKey(username).email,
        "gender": getKey(username).gender,
        "phone": getKey(username).phone,
        "password": password
    });
    localStorage.removeItem(username);
    localStorage.setItem(username, account);
}

// 显示信息
function printError(elemId, histMsg) {
    document.getElementById(elemId).innerHTML = histMsg;
}

// 从页面获取元素
function getDocValue(elemId) {
    return document.getElementById(elemId).value;
}

// 从浏览器获取数据
function getKey(key) {
    return JSON.parse(localStorage.getItem(key));
}